package com.cheer.ba.view.activity;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.Constants;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.MyTradeRecordModel;
import com.cheer.ba.presenter.impl.FindOilPresenter;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.TextViewUtils;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;
import com.cheer.ba.widget.XListView.XListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的交易记录
 */
public class LookMyTradeAty extends BaseCommAty implements XListView.IXListViewListener {

    @BindView(R.id.id_trade_listv)
    XListView mXListView;
    private CommonAdapter<MyTradeRecordModel> mTradeAdp;
    private List<MyTradeRecordModel> myTrades = new ArrayList<MyTradeRecordModel>();

    private int page_frome;
    //油卡id
    private String mCardId;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_look_my_trade, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new FindOilPresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return !myTrades.isEmpty();
    }

    @Override
    public void initView() {
        page_frome = getIntent().getIntExtra(IntentKey.General.KEY_PAGE, Constants.DEFAULTVALUE);
        if (page_frome == IntentCode.General.JUMP_DEVICE) {
            mCardId = getIntent().getStringExtra(IntentKey.General.KEY_ID);
            setTitleText("油卡变更记录");
        } else if (page_frome == IntentCode.General.JUMP_QYE) {
            setTitleText("交易记录");
        }
        mXListView.setPullRefreshEnable(true);
        mXListView.setPullLoadEnable(true);
        mXListView.setXListViewListener(this);
        mTradeAdp = new CommonAdapter<MyTradeRecordModel>(aty, myTrades, R.layout.adp_my_trade) {
            @Override
            public void convert(ViewHolder helper, MyTradeRecordModel item) {
                if (null != item) {
                    RelativeLayout mHeadView = (RelativeLayout) helper.getViewById(R.id.id_book_head_view);
                    if (StringUtils.isEmpty(item.month)) {
                        mHeadView.setVisibility(View.GONE);
                    } else {
                        mHeadView.setVisibility(View.VISIBLE);
                        if (StringUtils.isToyear(item.createTime)) {
                            helper.setText(R.id.id_book_month_txt, item.month + " 月");
                        } else {
                            helper.setText(R.id.id_book_month_txt, StringUtils.getDetailStr1(item.createTime));
                        }
                    }
                    //日期
                    TextView mTxtTime = (TextView) helper.getViewById(R.id.id_book_day_txt);
                    if (StringUtils.isToday(item.createTime)) {
                        mTxtTime.setText("今天");
                    } else if (StringUtils.isYesterday(item.createTime)) {
                        mTxtTime.setText("昨天");
                    } else {
                        mTxtTime.setText(StringUtils.getWeek(StringUtils.stampToDate(item.createTime, "yyyy-MM-dd")));
                    }
                    //时间
                    helper.setText(R.id.id_book_hour_txt, StringUtils.getDetailStr1(item.createTime));
                    String money = StringUtils.removePoint(Currency.returnDollar(Currency.RMB, item.money, 0).replace("元", "")) + "元";
                    if (item.logType == 1) {
                        money = "+" + money;
                        helper.setBackgroundResource(R.id.id_book_incream_img, R.drawable.ic_incream);
                        TextViewUtils.setSpannableStyleColor(money, 0, money.length() - 1, (TextView) helper.getViewById(R.id.id_book_money_txt), R.color.color_main);
                        helper.setText(R.id.id_book_type_txt, "收入");
                    } else {
                        money = "-" + money;
                        TextViewUtils.setSpannableStyleColor(money, 0, money.length() - 1, (TextView) helper.getViewById(R.id.id_book_money_txt), R.color.color_333333);
                        helper.setBackgroundResource(R.id.id_book_incream_img, R.drawable.ic_outcream);
                        helper.setText(R.id.id_book_type_txt, "支出");
                    }
                }
            }
        };
        mXListView.setAdapter(mTradeAdp);
    }

    @Override
    public void initData() {
        if (page_frome == IntentCode.General.JUMP_DEVICE) {
            ((FindOilPresenter) presenter).getMyDevice(hasDataInPage(), false, mCardId);
        } else if (page_frome == IntentCode.General.JUMP_QYE) {
            ((FindOilPresenter) presenter).getMyTrades(hasDataInPage(), false);
        }
    }

    @Override
    public <T> void updateViewWithLoadMore(T t, boolean loadMore, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.GET_MY_TRADE)) {
                //油信息
                mXListView.stopRefresh();
                mXListView.stopLoadMore();
                List<MyTradeRecordModel> mOils = (List<MyTradeRecordModel>) t;
                if (!loadMore) {
                    myTrades.clear();
                }
                if (!mOils.isEmpty()) {
                    for (MyTradeRecordModel model : mOils) {
                        String m = StringUtils.getMonth(model.createTime);
                        if (StringUtils.isEmpty(m)) {
                            model.month = "";
                        } else {
                            model.month = (Integer.parseInt(m) + 1) + "";
                        }
                    }
                    String a = mOils.get(0).month;
                    for (int i = 1; i < mOils.size(); i++) {
                        MyTradeRecordModel model = mOils.get(i);
                        String m = model.month;
                        if (TextUtils.equals(a, m)) {
                            m = "";
                        } else {
                            a = m;
                        }
                        model.month = m;
                    }
                    myTrades.addAll(mOils);
                    mTradeAdp.notifyDataSetChanged();
                } else {
                    hideFoot();
                    if (loadMore) {
                        ToastUtil.showToast(getString(R.string.no_more_data));
                    }
                }
            }
        }
    }

    @Override
    public void onRefresh() {
        if (mXListView.getFooterViewsCount() <= 0) {
            mXListView.addFooterView(mXListView.mFooterView);
        }
        if (page_frome == IntentCode.General.JUMP_DEVICE) {
            ((FindOilPresenter) presenter).getMyDevice(hasDataInPage(), false, mCardId);
        } else if (page_frome == IntentCode.General.JUMP_QYE) {
            ((FindOilPresenter) presenter).getMyTrades(hasDataInPage(), false);
        }
    }

    @Override
    public void onLoadMore() {
        if (page_frome == IntentCode.General.JUMP_DEVICE) {
            ((FindOilPresenter) presenter).getMyDevice(hasDataInPage(), true, mCardId);
        } else if (page_frome == IntentCode.General.JUMP_QYE) {
            ((FindOilPresenter) presenter).getMyTrades(hasDataInPage(), true);
        }
    }

    private void hideFoot() {
        mXListView.removeFooterView(mXListView.getFooterView());
    }

    @Override
    public void handlerByPage(int flag1, int flag2, boolean loadMore) {
        mXListView.stopRefresh();
        mXListView.stopLoadMore();
        switch (flag1) {
            case CodeStatus.Gegeneral.ERROR_NET:
                ToastUtil.showToast("无网络");
                break;
            case CodeStatus.Gegeneral.ERROR_DATA:
                ToastUtil.showToast("获取失败");
                break;
        }
    }
}
